import axios from 'axios'
import {getToken, localToken} from "../token/token";
import {login} from "./api";
import {url} from '../token/token'
// const BASE_URL = "http://192.168.1.2:8010/";
const service = axios.create({
    baseURL:url,
})

// request拦截器
service.interceptors.request.use(config => {

    // get请求映射params参数
    if (config.method === 'get' && config.params) {
        let url = config.url + '?';
        for (const propName of Object.keys(config.params)) {
            const value = config.params[propName];
            var part = encodeURIComponent(propName) + "=";
            if (value !== null && typeof (value) !== "undefined") {
                if (typeof value === 'object') {
                    for (const key of Object.keys(value)) {
                        let params = propName + '[' + key + ']';
                        var subPart = encodeURIComponent(params) + "=";
                        url += subPart + encodeURIComponent(value[key]) + "&";
                    }
                } else {
                    url += part + encodeURIComponent(value) + "&";
                }
            }
        }
        url = url.slice(0, -1);
        config.params = {};
        config.url = url;
    }
    /**有一些轻量请求 多频率的可以加到这里面 不显示模态框遮挡 */
    localToken()
    if (getToken() !== ''){
        // config.headers.token = getToken();
        // @ts-ignore
        config.headers['token'] = getToken();
    }
    return config
}, error => {
    console.log(error)
    Promise.reject(error)
})

// 响应拦截器
service.interceptors.response.use(res => {
    // console.log(loadingFull);
    return res.data

},
    error => {
        console.log('err' + error)
        let { message } = error;
        if (error.response.status == 401) {

        }
        else if (message == "Network Error") {
            message = "后端接口连接异常";
        }
        else if (message.includes("timeout")) {
            message = "系统接口请求超时";
        }
        return Promise.reject(error)
    }
)

export default service
